<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link href="print-lock.css" rel="stylesheet" />
    <link href="print-lock.css" media="print" rel="stylesheet" />
    <script>
      window.$ = window.jQuery = require("jquery");
      $.fn.onImgLoaded = (callback) => {
        let cb = (len) => {
          if (len <= 0) {
            callback();
          }
        };
        let len = $("img").length;
        cb(len);
        let getUrl = (str) => {
          let reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
          let v = str.match(reg);
          if (v && v.length) {
            return v[0];
          }
          return "";
        };
        $("img").each((i, e) => {
          let $img = $(e);
          let img = new Image();
          let src = $img.attr("src");
          if (!new RegExp("[a-zA-Z]+://[^\s]*").test(src)) {
            src = getUrl($img.attr("style") || "");
          }
          img.src = src;
          if (img.complete || src == "") {
            len--;
            cb(len);
          } else {
            img.onload = () => {
              len--;
              cb(len);
            };
            img.onerror = () => {
              len--;
              cb(len);
            };
          }
        });
      };
    </script>
  </head>

  <body>
    <div id="printElement"></div>
    <script>
      const { ipcRenderer, BrowserWindow } = require("electron");

      $(document).ready(function() {
        ipcRenderer.on("print-new", (event, data) => {
          document.title = data.title ? data.title : "hiprint打印";
          document.getElementById("printElement").innerHTML = data.html;
          data.pageNum = $(".hiprint-printPaper").length;
          $.fn.onImgLoaded(() => {
            ipcRenderer.send("do", data);
          });
        });
      });
    </script>
  </body>
</html>
